package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zfj
 * @date 2024/4/7 -14:19
 */
@Service
public class HospitalServiceImpl implements HospitalService {
	@Autowired
	private HospitalRepository hospitalRepository;

	@Autowired
	private DictFeignClient dictFeignClient;

	@Override
	public void save(Map<String, Object> paramMap) {
		//把map集合转换为对象
		String mapString = JSONObject.toJSONString(paramMap);
		Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
		//添加或更新
		//判断是否存在相通数据
		String hoscode = hospital.getHoscode();
		Hospital hospitalExit=hospitalRepository.getHospitalByHoscode(hoscode);
		//存在更新
		if(hospitalExit!=null){
			hospital.setStatus(hospitalExit.getStatus());
			hospital.setCreateTime(hospitalExit.getCreateTime());
			hospital.setUpdateTime(new Date());
			hospital.setIsDeleted(0);
			hospitalRepository.save(hospital);
		}else {
			//不存在添加
			hospital.setStatus(0);
			hospital.setCreateTime(new Date());
			hospital.setUpdateTime(new Date());
			hospital.setIsDeleted(0);
			hospitalRepository.save(hospital);
		}


	}

	@Override
	public Hospital getByHoscode(String hoscode) {
		Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
		return hospital;
	}

	/**
	 * 条件查询带分页**/
	@Override
	public Page selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
		//创建Pageable
		//Pageable pageable= PageRequest.of(page-1,limit);
		Pageable pageable= PageRequest.of(page-1,limit);
		//创建条件匹配器
		Hospital hospital = new Hospital();
		BeanUtils.copyProperties(hospitalQueryVo, hospital);

		// 创建匹配器，即如何使用查询条件 // 构建对象
		ExampleMatcher matcher = ExampleMatcher.matching()
				// 改变默认字符串匹配方式：模糊查询
				.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
				// 改变默认大小写忽略方式：忽略大小写
				.withIgnoreCase(true);

		// 创建实例
		Example<Hospital> example = Example.of(hospital, matcher);

		Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

		//获取查询list集合，便利进行医院等级封装
		pages.getContent().stream().forEach(item->{
			this.setHospitalHosType(item);
		});

		return pages;
	}

	@Override
	public void updateStatus(String id, Integer status) {
		if (status == 0 || status == 1) {
			Hospital hospital = hospitalRepository.findById(id).get();
			hospital.setStatus(status);
			hospital.setUpdateTime(new Date());
			hospitalRepository.save(hospital);
		}
	}

	@Override
	public Map<String, Object> getHospById(String id) {
		Map<String, Object> result = new HashMap<>();

		Hospital hospital = hospitalRepository.findById(id).get();
		this.setHospitalHosType(hospital);
		result.put("hospital", hospital);

		//单独处理更直观
		result.put("bookingRule", hospital.getBookingRule());
		//不需要重复返回
		hospital.setBookingRule(null);
		return result;
	}

	@Override
	public String getHospName(String hoscode) {
		Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
		if(hospital !=null){
			return hospital.getHosname();
		}
		return null;
	}

	//获取查询的list集合，遍历进行医院等级的封装
	private Hospital setHospitalHosType(Hospital hospital) {
		String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
		//查询省 市 地区
		String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
		String cityString = dictFeignClient.getName(hospital.getCityCode());
		String districtString = dictFeignClient.getName(hospital.getDistrictCode());
		hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
		hospital.getParam().put("hostypeString",hostypeString);
		return hospital;
	}
}
